<style>
    .model-form .layui-form-label {
        width: 110px;
    }
    .model-form .layui-input-block {
        margin-left: 140px;
    }
    .cardU{
        background-image: url(../../../admin/assets/images/card_u.png);
        background-repeat: no-repeat;
        background-position: center;
        width: calc(100% - 142px);
        height: 150px;
        padding: 0;
    }
    .cardU img{width: 100%;height: 100%;}
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">性质：</label>
                                    <div class="layui-input-inline mr0">
                                        <select name="nature">
                                            <option value="">请选择性质</option>
                                            <option value="enterprise">企业</option>
                                            <option value="individual">个体工商</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">名称：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="name" class="layui-input" type="text" placeholder="请输入名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit>查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="transportCompanyBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">
<!--                            <button id="transportCompanyBtnExport" class="layui-btn icon-btn layui-btn-danger">导出</button>-->
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="transportCompanyTable" lay-filter="transportCompanyTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="transportCompanyTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="transportCompanyForm">
    <form lay-filter="transportCompanyForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>编码</label>
                <div class="layui-input-block">
                    <input name="code" placeholder="请输入编码" type="text" class="layui-input" lay-verify="required|isOnlySn" required/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>名称</label>
                <div class="layui-input-block">
                    <input name="name" placeholder="请输入名称" type="text" class="layui-input" lay-verify="required" required/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>性质</label>
                <div class="layui-input-block">
                    <select name="nature">
                        <option value="enterprise">企业</option>
                        <option value="individual">个体工商</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>营业执照号</label>
                <div class="layui-input-block">
                    <input name="licenseNo" placeholder="请输入营业执照号" type="text" class="layui-input" lay-verify="required" required/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6" style="height: 170px;">
            <div class="layui-form-item">
                <label class="layui-form-label">证件照片</label>
                <div class="layui-upload-drag cardU" id="photo">
                    <!--<i class="layui-icon"></i>
                    <p>点击上传，或将文件拖拽到此处</p>-->
                    <input type="hidden" name="photo" lay-verify="" data-name="证件照片" >
                </div>
            </div>
        </div>
        <div class="layui-col-xs6" style="height: 170px;">
            <div class="layui-form-item">
                <label class="layui-form-label">LOGO</label>
                <div class="layui-upload-drag cardU" id="logo">
                    <!--<i class="layui-icon"></i>
                    <p>点击上传，或将文件拖拽到此处</p>-->
                    <input type="hidden" name="logo" lay-verify="" data-name="LOGO" >
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>联系人</label>
                <div class="layui-input-block">
                    <input name="contact" placeholder="请输入联系人" type="text" class="layui-input" lay-verify="required" required/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>手机号</label>
                <div class="layui-input-block">
                    <input name="phone" placeholder="请输入手机号" type="text" class="layui-input" lay-verify="required|mobile" required/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>是否启用</label>
                <div class="layui-input-block">
                    <input name="isDeleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入备注" maxlength="200" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="transportCompanyFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#transportCompanyTable',
            url: config.base + 'enterprise/invoicing/admin/transportCompany/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号', width: 80}
                ,{field: 'code', align:'center', title: '编码'}
                ,{field: 'name', align:'center', title: '名称'}
                ,{field: 'natureDesc', align:'center', title: '性质'}
                ,{field: 'contact', align:'center', title: '联系人'}
                ,{field: 'phone', align:'center', title: '手机号'}
                ,{field: 'createTime', align:'center', title: '创建时间'}
                ,{align: 'center', toolbar: '#transportCompanyTableBar', title: '操作', width: 200}
            ]]
        });

        // 工具条点击事件
        table.on('tool(transportCompanyTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(transportCompanyTable)', function(obj){
            table.reload('transportCompanyTable', {
                initSort: obj,
                where: {
                    sort: obj.field,
                    order: obj.type
                }
            });
        });

        //渲染下拉框
        form.render('select');

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-query-box").find('select').val("");
            $(".lay-query-box").find('input').val("");
            var field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#transportCompanyBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '50%',
                offset: '150px',
                title: data ? '修改运输公司' : '添加运输公司',
                content: $('#transportCompanyForm').html(),
                success: function () {
                    form.verify({
                        isOnlySn: function (value) {
                            var isOnly = true;
                            var idVal = $("form input[name='id']").val();
                            admin.req('enterprise/invoicing/admin/transportCompany/isOnly', {code:value,id:idVal}, function (res) {
                                if (res.code == 200) {
                                    isOnly = res.data;
                                }
                            }, 'GET',false);
                            if(!isOnly){
                                return "编码必须唯一"
                            }
                        },
                        mobile: [/^(1(3|4|5|7|8|9)\d{9}|[ \t]*)$/, '手机号码格式错误']
                    })

                    if(data){
                        var logo = data.logo;
                        if(logo){
                            $("#logo").html('<img src="'+ logo+'" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="logo" value="'+logo+'">');
                        }

                        var photo = data.photo;
                        if(photo){
                            $("#photo").html('<img src="'+ photo+'" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="photo" value="'+photo+'">');
                        }

                        // 性质
                        var natureOption = $("form select[name='nature']").find("option");
                        layui.each(natureOption, function(index, item){
                            if(data.nature == item.text){
                                data.nature = item.value;
                            }
                        });
                    }

                    //拖拽上传证件照片
                    upload.render({
                        elem: '#photo'
                        ,url: config.upload_url + 'upload?path='+img_path
                        , auto: true
                        , acceptMime: 'image/*'
                        ,headers:{authorization:'Bearer '+config.getToken()}
                        ,done: function(res){
                            if(res.code == 200){
                                layer.msg("上传成功", {icon: 1});
                                var imagUrl=res.data.url;
                                /*if(imagUrl){
                                    imagUrl=imagUrl.substring(imagUrl.indexOf('/')+1)
                                }*/
                                $("#photo").html('<img src="'+imagUrl+'" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="photo" value="'+imagUrl+'">');
                            }else if(res.code == 502){
                                return location.replace('admin/login.html');
                            }else{
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });

                    //拖拽上传LOGO
                    upload.render({
                        elem: '#logo'
                        ,url: config.upload_url + 'upload?path='+img_path
                        , auto: true
                        , acceptMime: 'image/*'
                        ,headers:{authorization:'Bearer '+config.getToken()}
                        ,done: function(res){
                            if(res.code == 200){
                                layer.msg("上传成功", {icon: 1});
                                var imagUrl=res.data.url;
                                /*if(imagUrl){
                                    imagUrl=imagUrl.substring(imagUrl.indexOf('/')+1)
                                }*/
                                $("#logo").html('<img src="'+imagUrl+'" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="logo" value="'+imagUrl+'">');
                            }else if(res.code == 502){
                                return location.replace('admin/login.html');
                            }else{
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });
                    form.val('transportCompanyForm', data);
                    // 表单提交事件
                    form.on('submit(transportCompanyFormSubmit)', function (d) {
                        layer.load(2);
                        var isDeleted = $("form input[name='isDeleted']").prop("checked");
                        if(!isDeleted){
                            d.field.isDeleted=false;
                        }else{
                            d.field.isDeleted=true;
                        }
                        admin.req(data ? 'enterprise/invoicing/admin/transportCompany/update' : 'enterprise/invoicing/admin/transportCompany/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('transportCompanyTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/invoicing/admin/transportCompany/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#transportCompanyBtnExport').click(function () {
            var checkRows = table.checkStatus('transportCompanyTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>